<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="iOS,Storyboard," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="情景为了布局结构的清晰，我们常把一些控件放入同一个UIView中。比如以下布局，一个UIView(蓝色部分)作为容器，容纳一个UIImageView和两个UILabel。

这样在对其设置隐藏时也是相当方便，我们不需要分别设置3个子控件的hidden属性，而只需设置容器的hidden属性。但是，contentLabel的行数一旦增多，可能超出容器范围。">
<meta property="og:type" content="article">
<meta property="og:title" content="UIView自适应高度">
<meta property="og:url" content="http://yoursite.com/2016/01/14/iOS/UIView自适应高度/index.html">
<meta property="og:site_name" content="ZhouJiatao's Blog">
<meta property="og:description" content="情景为了布局结构的清晰，我们常把一些控件放入同一个UIView中。比如以下布局，一个UIView(蓝色部分)作为容器，容纳一个UIImageView和两个UILabel。

这样在对其设置隐藏时也是相当方便，我们不需要分别设置3个子控件的hidden属性，而只需设置容器的hidden属性。但是，contentLabel的行数一旦增多，可能超出容器范围。">
<meta property="og:image" content="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23normalLayout.jpeg">
<meta property="og:image" content="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23leakageLayout.jpeg">
<meta property="og:image" content="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23demoLayout.png">
<meta property="og:image" content="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23UIViewConstraints.png">
<meta property="og:image" content="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23UILabelConstraints.png">
<meta property="og:image" content="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23updateFrame.png">
<meta property="og:updated_time" content="2017-01-13T04:46:12.189Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="UIView自适应高度">
<meta name="twitter:description" content="情景为了布局结构的清晰，我们常把一些控件放入同一个UIView中。比如以下布局，一个UIView(蓝色部分)作为容器，容纳一个UIImageView和两个UILabel。

这样在对其设置隐藏时也是相当方便，我们不需要分别设置3个子控件的hidden属性，而只需设置容器的hidden属性。但是，contentLabel的行数一旦增多，可能超出容器范围。">
<meta name="twitter:image" content="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23normalLayout.jpeg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/01/14/iOS/UIView自适应高度/"/>





  <title> UIView自适应高度 | ZhouJiatao's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  


<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-91431386-1', 'auto');
  ga('send', 'pageview');
</script>









  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">ZhouJiatao's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle"></p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/01/14/iOS/UIView自适应高度/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="ZhouJiatao">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://7xr5jb.com1.z0.glb.clouddn.com/avatar%23privateinvestocat.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="ZhouJiatao's Blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="ZhouJiatao's Blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                UIView自适应高度
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-01-14T09:14:50+00:00">
                2016-01-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/iOS-How-To/" itemprop="url" rel="index">
                    <span itemprop="name">iOS How-To</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/01/14/iOS/UIView自适应高度/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/01/14/iOS/UIView自适应高度/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="情景"><a href="#情景" class="headerlink" title="情景"></a>情景</h1><p>为了布局结构的清晰，我们常把一些控件放入同一个UIView中。<br>比如以下布局，一个UIView(蓝色部分)作为容器，容纳一个UIImageView和两个UILabel。</p>
<p><img src="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23normalLayout.jpeg" alt="normalLayout"></p>
<p>这样在对其设置隐藏时也是相当方便，我们不需要分别设置3个子控件的hidden属性，而只需设置容器的hidden属性。<br>但是，contentLabel的行数一旦增多，可能超出容器范围。<br><a id="more"></a><br><img src="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23leakageLayout.jpeg" alt="leakageLayout"><br><em>(Tips：将UILabel的lines属性设置为0，就不会被限制行数)</em></p>
<p>我们怎样才能让UIView随contentLabel的内容而改变高度呢？</p>
<h1 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h1><p>以上布局是为了让你理解，通常来说将几个控件归纳进一个容器，是多么有必要。现在，为了更方便的进行下一步演示，我们将布局简化为一个UIView包容一个UILabel。</p>
<p><img src="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23demoLayout.png" alt="demoLayout"></p>
<p>为了让UIView适应内容的高度，需要进行以下步骤：</p>
<h3 id="1-UIView’s-height-constraint-priority"><a href="#1-UIView’s-height-constraint-priority" class="headerlink" title="1.UIView’s height constraint priority"></a>1.UIView’s height constraint priority</h3><p>将UIView的height constraint 的Priority设置得比UILabel的”Content Hugging Priority”和”Content Compression Resistance Priority”都低。（这里设置了249）<br><img src="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23UIViewConstraints.png" alt="UIViewConstraints"></p>
<h3 id="2-UILabel-Bottom-space-to-container"><a href="#2-UILabel-Bottom-space-to-container" class="headerlink" title="2. UILabel: Bottom space to container"></a>2. UILabel: Bottom space to container</h3><p>需要约束UILabel的底部和容器的距离<br><img src="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23UILabelConstraints.png" alt="UILabelConstraints"></p>
<h3 id="3-update-frames"><a href="#3-update-frames" class="headerlink" title="3. update frames"></a>3. update frames</h3><p><img src="http://7xr5jb.com1.z0.glb.clouddn.com/containerAdaptive%23updateFrame.png" alt="updateFrame"></p>
<p>OK！你可以在storyboard中立即看到效果，UIView高度增高，UILabel内容也得以完全显示。在程序运行时，修改UILabel的内容，UIview的高度也会随之变化。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/iOS/" rel="tag"># iOS</a>
          
            <a href="/tags/Storyboard/" rel="tag"># Storyboard</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/12/30/iOS/图片透明层引发的边距问题/" rel="next" title="图片透明层引发的边距问题">
                <i class="fa fa-chevron-left"></i> 图片透明层引发的边距问题
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/01/15/iOS/如何清空NSUserDefaults/" rel="prev" title="如何清空NSUserDefaults">
                如何清空NSUserDefaults <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/01/14/iOS/UIView自适应高度/"
           data-title="UIView自适应高度" data-url="http://yoursite.com/2016/01/14/iOS/UIView自适应高度/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://7xr5jb.com1.z0.glb.clouddn.com/avatar%23privateinvestocat.jpg"
               alt="ZhouJiatao" />
          <p class="site-author-name" itemprop="name">ZhouJiatao</p>
          <p class="site-description motion-element" itemprop="description">Write simple code</p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">18</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/RealMeZJT" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#情景"><span class="nav-number">1.</span> <span class="nav-text">情景</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#解决方法"><span class="nav-number">2.</span> <span class="nav-text">解决方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-UIView’s-height-constraint-priority"><span class="nav-number">2.0.1.</span> <span class="nav-text">1.UIView’s height constraint priority</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-UILabel-Bottom-space-to-container"><span class="nav-number">2.0.2.</span> <span class="nav-text">2. UILabel: Bottom space to container</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-update-frames"><span class="nav-number">2.0.3.</span> <span class="nav-text">3. update frames</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ZhouJiatao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"realmezjt"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  












  
  

  

  

  

  


</body>
</html>
